﻿package com.tudou.newPlayer
{
    import com.tudou.model.*;
    import com.tudou.utils.*;
    import flash.events.*;
    import flash.utils.*;

    public class PreviewPlayer extends VideoPlayer
    {
        protected var previewURL:String;
        protected var beginTime:Number = 0;
        protected var previewMetaData:MetaData;
        protected var timeoutId:uint;
        public static const READY:String = "preview ready!";

        public function PreviewPlayer(param1:uint = 400, param2:uint = 300)
        {
            super(param1, param2);
            mute = true;
            playerTimer.stop();
            return;
        }// end function

        override protected function changeStatus(param1:int) : void
        {
            if (param1 == VideoStatus.READY)
            {
                dispatchEvent(new Event(READY));
            }
            super.changeStatus(param1);
            return;
        }// end function

        public function preview(param1:Number) : void
        {
            var _loc_2:String = null;
            var _loc_3:Number = NaN;
            var _loc_4:KeyInfo = null;
            var _loc_5:KeyInfo = null;
            var _loc_6:Number = NaN;
            var _loc_7:Number = NaN;
            if (previewURL && previewMetaData)
            {
                _loc_2 = previewURL;
                _loc_3 = previewMetaData.totalTime * param1;
                _loc_4 = previewMetaData.getKeyPointByTime(_loc_3);
                _loc_5 = previewMetaData.getKeyPointByTime(_loc_3, 3);
                _loc_6 = Math.ceil(previewMetaData.isMP4 ? (_loc_4.time) : (_loc_4.size));
                _loc_7 = Math.ceil(previewMetaData.isMP4 ? (_loc_5.time) : (_loc_5.size));
                beginTime = _loc_4.time;
                playerTimer.start();
                _loc_2 = URLUtil.addKeyValue(_loc_2, "tflvbegin", _loc_6.toString());
                if (_loc_7 > _loc_6)
                {
                    _loc_2 = URLUtil.addKeyValue(_loc_2, "tflvend", _loc_7.toString());
                }
                _loc_2 = URLUtil.addKeyValue(_loc_2, "igrft", RandString.noCache);
                load(_loc_2);
                play();
            }
            return;
        }// end function

        override protected function playerTimerHlr(event:TimerEvent) : void
        {
            if (playTime - beginTime >= 3)
            {
                timeoutId = setTimeout(timeoutHlr, 1500);
                seek(0);
                pause();
            }
            return;
        }// end function

        protected function timeoutHlr() : void
        {
            clearTimeout(timeoutId);
            timeoutId = 0;
            resume();
            return;
        }// end function

        public function init(param1:String, param2:MetaData) : void
        {
            previewURL = param1;
            previewMetaData = param2;
            _orginalVideoScale = previewMetaData.width / previewMetaData.height;
            if (isNaN(_orginalVideoScale) || _orginalVideoScale <= 0)
            {
                _orginalVideoScale = 4 / 3;
            }
            videoScale = _orginalVideoScale;
            return;
        }// end function

        override public function close() : void
        {
            clearTimeout(timeoutId);
            timeoutId = 0;
            super.close();
            playerTimer.stop();
            return;
        }// end function

    }
}
